package com.usc.mock.db.service.impl;

import com.usc.mock.db.bean.FavorInfo;
import com.usc.mock.db.mapper.FavorInfoMapper;
import com.usc.mock.db.mapper.SkuInfoMapper;
import com.usc.mock.db.mapper.UserInfoMapper;
import com.usc.mock.db.service.FavorInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usc.mock.utlil.ParamUtil;
import com.usc.mock.utlil.RandomCollection;
import com.usc.mock.utlil.RandomNum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品收藏表 服务实现类
 * </p>
 */
@Service
@Slf4j
public class FavorInfoServiceImpl extends ServiceImpl<FavorInfoMapper, FavorInfo> implements FavorInfoService {

    @Autowired
    SkuInfoMapper skuInfoMapper;

    @Autowired
    UserInfoMapper userInfoMapper;


    @Value("${mock.date}")
    String mockDate;

    @Value("${mock.favor.count}")
    String countString;

    @Value("30")
    String cancelRate;

    public void genFavors(Boolean ifClear) {
        Integer count = ParamUtil.checkCount(countString);
        Date date = ParamUtil.checkDate(mockDate);
        if (ifClear) {
            remove(new QueryWrapper<>());
        }
        Integer skuTotal = skuInfoMapper.selectCount(new QueryWrapper<>());
        Integer userTotal = userInfoMapper.selectCount(new QueryWrapper<>());

        List<FavorInfo> favorInfoList = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            Long userId = RandomNum.getRandInt(1, userTotal) + 0L;
            Long skuId = RandomNum.getRandInt(1, skuTotal) + 0L;
            favorInfoList.add(initFavorInfo(date, skuId, userId));
        }
        saveBatch(favorInfoList, 100);
        log.warn("共生成收藏" + favorInfoList.size() + "条");

        cancelFavorInfo(DateUtils.addSeconds(date, 30));
    }


    public FavorInfo initFavorInfo(Date date, Long skuId, Long userId) {

        FavorInfo favorInfo = new FavorInfo();
        favorInfo.setIsCancel("0");
        favorInfo.setSkuId(skuId);
        favorInfo.setUserId(userId);
        favorInfo.setCreateTime(date);
        return favorInfo;
    }

    public void cancelFavorInfo(Date date) {
        List<FavorInfo> favorInfoList = this.list(new QueryWrapper<FavorInfo>().eq("is_cancel", "0"));
        Integer cancelRatio = ParamUtil.checkRatioNum(cancelRate);
        int cancelCount = (cancelRatio * 100) / favorInfoList.size();

        List<FavorInfo> cancelFavorList = RandomCollection.getSomeFrom(favorInfoList, cancelCount);
        for (FavorInfo favorInfo : cancelFavorList) {
            favorInfo.setIsCancel("1");
            favorInfo.setCancelTime(date);
        }

        saveOrUpdateBatch(cancelFavorList, 1000);
        log.warn("共取消收藏" + cancelFavorList.size() + "条");

    }
}
